package com.useeinfo.asg.modules.logging.dao;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.entity.Resource;
import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by GeniusJ on 2017-05-12.
 */
@Repository()
public class ResourceDao implements CrudDao<Resource>{
    private final static Logger logger = LoggerFactory.getLogger(ResourceDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    // ******************************************************************************
    // ******************************* 代码自动生成开始 *******************************
    // ******************************************************************************

    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {

        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);

        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        for (String queryKey : queryHash.keySet()) {
            String queryValue = queryHash.get(queryKey);

            switch (queryKey) {
                case "id":      // id
                    conditionHash.put("s.id = ?{paramIndex} ", CommUtil.null2Long(queryValue));
                    continue;

            }
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {

        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from Resource s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<Resource> findList(QueryParam queryParam) {

        String sqlInfo = queryParam.joinJPQL("select s from Resource s ", "order by s.createTime desc ");

        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<Resource> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, Resource.class);

        return queryParam.findPageList(typedQuery);
    }

    @Override
    public Resource findModel(Long id) {
        return entityManager.find(Resource.class, id);
    }

    @Override
    public Integer add(Resource resource) {
        entityManager.persist(resource);
        logger.info("ResourceDaoImpl添加Resource成功！");
        return 1;
    }

    @Override
    public Integer update(Resource resource) {

        entityManager.merge(resource);
        return 1;
    }


    @Override
    public Integer delete(Long id) {
        Resource resource = entityManager.find(Resource.class, id);
        entityManager.remove(resource);
        return 1;
    }

    @Override
    public Integer delete_logic(Resource model) {
        Resource resource = entityManager.find(Resource.class, model.getId());
        resource.setDeleteStatus(true);
        return  1;
    }

    // ******************************************************************************
    // ******************************* 代码自动生成结束 *******************************
    // ******************************************************************************

    public List<Resource> findObjects(String jpql, Map<String, Object> params) {

        Query query =  entityManager.createQuery(jpql);
        if(params!=null) {
            for (String key : params.keySet()) {
                query.setParameter(key, params.get(key));
            }
        }
        return query.getResultList();
    }

    public List<Object> findListByNative(String sql) {

        Query query = entityManager.createNativeQuery(sql);
        return query.getResultList();
    }
}
